﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace QuestionManager
{
    class PushBussiness
    {
        private static PushBussiness instance;
        public static PushBussiness getInstace()
        {
            if (instance == null) instance = new PushBussiness();
            return instance;
        }

        public string GetProperty(List<QUESTION_INFO> _question_info, string infoName)
        {
            foreach(QUESTION_INFO item in _question_info)
            {
                if (item.QUESTION_INFO_PROP.Trim().ToLower().Equals(infoName.Trim().ToLower()))
                {
                    return item.QUESTION_INFO_VALUE;
                }
            }
            return "";
        }

        public decimal GetPropertyID(List<QUESTION_INFO> _question_info, string infoName)
        {
            foreach (QUESTION_INFO item in _question_info)
            {
                if (item.QUESTION_INFO_PROP.Trim().ToLower().Equals(infoName.Trim().ToLower()))
                {
                    return item.QUESTION_INFO_ID;
                }
            }
            return -1;
        }

        public bool UpdateQuestion(string userID, QuestionItem questionItem, List<AnswerItem> answerItems, Dictionary<string, object> DataQuestionToUpDate)
        {
            if (questionItem == null || answerItems == null) return false;
            try
            {
                QUESTION _questionOld = DataQuestionToUpDate["QUESTION"] as QUESTION;

                QUESTION _question = new QUESTION();
                _question.QUESTION_ID = _questionOld.QUESTION_ID;
                _question.USER_ID = userID;
                _question.QUESTION_TYPE = questionItem.Type;
                _question.QUESTION_LEVEL = questionItem.Diffculty.ID.ToString();
                _question.QUESTION_DESCRIPTION = questionItem.QuestionContent;

                //get question ID
                DAO_Question.Update(_question);

                List<QUESTION_INFO> _question_info = DataQuestionToUpDate["List_QUESTION_INFO"] as List<QUESTION_INFO>;
                //Course and time
                //Update couse
                QUESTION_INFO infoCourse = new QUESTION_INFO();
                infoCourse.QUESTION_ID = _question.QUESTION_ID;
                infoCourse.QUESTION_INFO_ID = GetPropertyID(_question_info, "COURSE");
                infoCourse.QUESTION_INFO_PROP = "COURSE";
                infoCourse.QUESTION_INFO_VALUE = questionItem.Course.ID.ToString();
                DAO_Question_Info.Update(infoCourse);
                QUESTION_INFO infoTime = new QUESTION_INFO();
                infoTime.QUESTION_INFO_ID = GetPropertyID(_question_info, "TIME");
                infoTime.QUESTION_ID = _question.QUESTION_ID;
                infoTime.QUESTION_INFO_PROP = "TIME";
                infoTime.QUESTION_INFO_VALUE = questionItem.Time.ID.ToString();
                DAO_Question_Info.Update(infoTime);
                QUESTION_INFO infoProfessorName = new QUESTION_INFO();
                infoProfessorName.QUESTION_ID = _question.QUESTION_ID;
                infoProfessorName.QUESTION_INFO_ID = GetPropertyID(_question_info, "PROFESSOR_NAME");
                infoProfessorName.QUESTION_INFO_PROP = "PROFESSOR_NAME";
                infoProfessorName.QUESTION_INFO_VALUE = questionItem.Professor.Name;
                DAO_Question_Info.Update(infoProfessorName);
                QUESTION_INFO infoProfessorID = new QUESTION_INFO();
                infoProfessorID.QUESTION_ID = _question.QUESTION_ID;
                infoProfessorID.QUESTION_INFO_ID = GetPropertyID(_question_info, "PROFESSOR_ID");
                infoProfessorID.QUESTION_INFO_PROP = "PROFESSOR_ID";
                infoProfessorID.QUESTION_INFO_VALUE = questionItem.Professor.ID.ToString();
                DAO_Question_Info.Update(infoProfessorID);
                //===================================================================

                FIGURELINK _figureLinkOld = DataQuestionToUpDate["FIGURELINK"] as FIGURELINK;
                //insert figure of Question
                FIGURELINK _figureLink = new FIGURELINK();
                //double
                _figureLink.FIGURE_ID = _figureLinkOld.FIGURE_ID;
                _figureLink.FIGURE_DESC = "Image";
                _figureLink.FIGURE_PATH = questionItem.ImagePath;
                DAO_FigureLink.Update(_figureLink);

                List<QUESTION_ITEM> _question_item = DataQuestionToUpDate["List_QUESTION_ITEM"] as List<QUESTION_ITEM>;
                List<QUESTION_ITEM_FIGURE> _question_item_figure = DataQuestionToUpDate["List_QUESTION_ITEM_FIGURE"] as List<QUESTION_ITEM_FIGURE>;
                List<QUESTION_SOLUTION> _question_item_solusion = DataQuestionToUpDate["List_QUESTION_SOLUTION"] as List<QUESTION_SOLUTION>;
                List<FIGURELINK> _question_item_figureLink = DataQuestionToUpDate["List_FIGURELINK"] as List<FIGURELINK>;

                QUESTION_FIGURE _questionFigureOld = DataQuestionToUpDate["QUESTION_FIGURE"] as QUESTION_FIGURE;
                QUESTION_FIGURE _questionFigure = new QUESTION_FIGURE();
                _questionFigure.QUESTION_FIGURE_ID = _questionFigureOld.QUESTION_FIGURE_ID;
                _questionFigure.FIGURE_ID = _figureLink.FIGURE_ID;
                _questionFigure.QUESTION_FIGURE_DESC = "Image";
                _questionFigure.QUESTION_ID = _question.QUESTION_ID;
                DAO_Question_Figure.Update(_questionFigure);

                for (int i = 0 ; i < answerItems.Count ; i++)
                {
                    var answerItem = answerItems[i];
                    QUESTION_ITEM _questionItem = new QUESTION_ITEM();
                    _questionItem.ITEM_ID = _question_item[i].ITEM_ID;
                    _questionItem.QUESTION_ID = _question.QUESTION_ID;
                    _questionItem.ITEM_FACE = answerItem.TextContent;
                    _questionItem.ITEM_VALUE = answerItem.Correct.ToString();
                    //get _questionItem ID                    
                    DAO_Question_Item.Update(_questionItem);

                    FIGURELINK _figureLinkAns = new FIGURELINK();
                    //double
                    _figureLinkAns.FIGURE_ID = int.Parse(_question_item_figure[i].FIGURE_ID.ToString());
                    _figureLinkAns.FIGURE_DESC = "Image";
                    _figureLinkAns.FIGURE_PATH = answerItem.ImagePath;
                    DAO_FigureLink.Update(_figureLinkAns);

                    QUESTION_ITEM_FIGURE _questionItemFigure = new QUESTION_ITEM_FIGURE();
                    _questionItemFigure.ITEM_ID = _question_item_figure[i].FIGURE_ID;
                    _questionItemFigure.ITEM_ID = _questionItem.ITEM_ID;
                    _questionItemFigure.ITEM_FIGURE_DESC = answerItem.ImagePath;
                    _questionItemFigure.FIGURE_ID = _figureLinkAns.FIGURE_ID;
                    DAO_Question_Item_Figure.Update(_questionItemFigure);

                    QUESTION_SOLUTION _solusion = new QUESTION_SOLUTION();
                    _solusion.SOLUTION_ID = _question_item_solusion[i].SOLUTION_ID;
                    _solusion.ITEM_ID = _questionItem.ITEM_ID;
                    _solusion.QUESTION_ID = _question.QUESTION_ID;
                    _solusion.SOLUTION_ORDER = (byte)0;
                    DAO_Question_Solution.Update(_solusion);
                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public bool PushQuestion(string userID, QuestionItem questionItem, List<AnswerItem> answerItems)
        {
            if (questionItem == null || answerItems == null) return false;
            try
            {
                QUESTION _question = new QUESTION();
                _question.USER_ID = userID;
                _question.QUESTION_TYPE = questionItem.Type;
                _question.QUESTION_LEVEL = questionItem.Diffculty.ID.ToString();
                _question.QUESTION_DESCRIPTION = questionItem.QuestionContent;                

                //get question ID
                DAO_Question.Insert(_question);
                string questionID = DAO_Question.CurrentInsertID();

                //Course and time
                //Insert couse
                QUESTION_INFO infoCourse = new QUESTION_INFO();
                infoCourse.QUESTION_ID = decimal.Parse(questionID);
                infoCourse.QUESTION_INFO_PROP = "COURSE";
                infoCourse.QUESTION_INFO_VALUE = questionItem.Course.ID.ToString();
                DAO_Question_Info.Insert(infoCourse);
                QUESTION_INFO infoTime = new QUESTION_INFO();
                infoTime.QUESTION_ID = decimal.Parse(questionID);
                infoTime.QUESTION_INFO_PROP = "TIME";
                infoTime.QUESTION_INFO_VALUE = questionItem.Time.ID.ToString();
                DAO_Question_Info.Insert(infoTime);
                QUESTION_INFO infoProfessorName = new QUESTION_INFO();
                infoProfessorName.QUESTION_ID = decimal.Parse(questionID);
                infoProfessorName.QUESTION_INFO_PROP = "PROFESSOR_NAME";
                infoProfessorName.QUESTION_INFO_VALUE = questionItem.Professor.Name;
                DAO_Question_Info.Insert(infoProfessorName);
                QUESTION_INFO infoProfessorID = new QUESTION_INFO();
                infoProfessorID.QUESTION_ID = decimal.Parse(questionID);
                infoProfessorID.QUESTION_INFO_PROP = "PROFESSOR_ID";
                infoProfessorID.QUESTION_INFO_VALUE = questionItem.Professor.ID.ToString();
                DAO_Question_Info.Insert(infoProfessorID);
                //===================================================================

                //insert figure of Question
                FIGURELINK _figureLink = new FIGURELINK();
                //double
                _figureLink.FIGURE_DESC = "Image";
                _figureLink.FIGURE_PATH = questionItem.ImagePath;
                DAO_FigureLink.Insert(_figureLink);
                //get figureID               
                string figureID = DAO_FigureLink.CurrentInsertID();

                QUESTION_FIGURE _questionFigure = new QUESTION_FIGURE();
                _questionFigure.FIGURE_ID = decimal.Parse(figureID);
                _questionFigure.QUESTION_FIGURE_DESC = "Image";
                _questionFigure.QUESTION_ID = decimal.Parse(questionID);
                DAO_Question_Figure.Insert(_questionFigure);

                foreach (var answerItem in answerItems)
                {
                    QUESTION_ITEM _questionItem = new QUESTION_ITEM();
                    _questionItem.QUESTION_ID = decimal.Parse(questionID);
                    _questionItem.ITEM_FACE = answerItem.TextContent;
                    _questionItem.ITEM_VALUE = answerItem.Correct.ToString();
                    //get _questionItem ID                    
                    DAO_Question_Item.Insert(_questionItem);
                    string _questionItemID = DAO_Question_Item.CurrentInsertID();

                    FIGURELINK _figureLinkAns = new FIGURELINK();
                    //double
                    _figureLinkAns.FIGURE_DESC = "Image";
                    _figureLinkAns.FIGURE_PATH = answerItem.ImagePath;
                    DAO_FigureLink.Insert(_figureLinkAns);
                    decimal figureIDAnswer = decimal.Parse(DAO_FigureLink.CurrentInsertID());

                    QUESTION_ITEM_FIGURE _questionItemFigure = new QUESTION_ITEM_FIGURE();
                    _questionItemFigure.ITEM_ID = decimal.Parse(_questionItemID);
                    _questionItemFigure.ITEM_FIGURE_DESC = answerItem.ImagePath;
                    _questionItemFigure.FIGURE_ID = figureIDAnswer;
                    DAO_Question_Item_Figure.Insert(_questionItemFigure);

                    QUESTION_SOLUTION _solusion = new QUESTION_SOLUTION();
                    _solusion.ITEM_ID = decimal.Parse(_questionItemID);
                    _solusion.QUESTION_ID = decimal.Parse(questionID);
                    _solusion.SOLUTION_ORDER = (byte)0;
                    DAO_Question_Solution.Insert(_solusion);
                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }
    }
}
